package com.jxrj.interaction.controller.teacher;

import com.alibaba.fastjson.JSONObject;
import com.jxrj.interaction.config.FileSaveAddress;
import com.jxrj.interaction.controller.BaseController;
import com.jxrj.interaction.entity.Teacher;
import com.jxrj.interaction.service.TeacherService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.UUID;

@Controller
public class TeacherUpdateController extends BaseController {
    @Resource(name = "teacherService")
    private TeacherService teacherService;
    @Autowired
    FileSaveAddress f;
    //修改昵称
    @ResponseBody
    @PostMapping("teacher/updateNickname")
    public String updateNickname(HttpSession session,@RequestBody Teacher o) {
        logger.info("教师修改昵称");
        JSONObject object = new JSONObject();
        if (o.getName()!=null && session.getAttribute("teacherId")!=null){
            teacherService.updateNickname(o.getName(), (Long) session.getAttribute("teacherId"));
            logger.info("成功修改昵称!");
            object.put("success",true);
        }else{
            logger.info("修改昵称失败!");
            object.put("success",false);
        }
        return object.toJSONString();
    }

    @ResponseBody
    @PostMapping("teacher/updatePersonalSign")
    public String updatePersonalSign(HttpSession session,@RequestBody Teacher o) {
        logger.info("教师修改签名");
        JSONObject object = new JSONObject();
        if (o.getSign()!=null && session.getAttribute("teacherId")!=null){
            teacherService.updatePersonalSign(o.getSign(), (Long) session.getAttribute("teacherId"));
            logger.info("成功修改签名!");
            object.put("success",true);
        }else{
            logger.info("修改签名失败!");
            object.put("success",false);
        }
        return object.toJSONString();
    }

    @ResponseBody
    @PostMapping("teacher/updateHeadIcon")
    public String updateHeadIcon(HttpSession session, MultipartFile fileUpload) {
        logger.info("教师修改头像");
        JSONObject object = new JSONObject();
        if (!fileUpload.isEmpty() && session.getAttribute("teacherId")!=null){
            //获取文件名
            String fileName = fileUpload.getOriginalFilename();
            //获取文件后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            //重新生成文件名
            fileName = UUID.randomUUID()+suffixName;

            Teacher teacher =teacherService.getTeacher((Long) session.getAttribute("teacherId"));
            try {
                //将图片保存到static文件夹里
                fileUpload.transferTo(new File( f.getFilePath()+fileName));
                //返回提示信息
                //数据库修改
                File file = new File(f.getFilePath()+teacher.getImg());
                //如果文件存在,就删除文件(即删除上一个头像)
                if (file.exists()){
                    file.delete();
                }
                teacherService.updateHeadIcon(fileName, (Long) session.getAttribute("teacherId"));
                logger.info("成功修改头像!");
                object.put("success",true);
            } catch (Exception e) {
                e.printStackTrace();
                logger.info("修改头像失败!");
                object.put("success",false);
                return object.toJSONString();
            }
        }else{
            logger.info("修改头像失败!");
            object.put("success",false);
        }
        return object.toJSONString();
    }
}
